package com.guoranhao.web.interceptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Description: 判断所有接口token有效性并封装用户信息
 * 要过滤掉不需要用户信息的接口（配置文件中配置）
 * Copyright (c) Department of Research and Development/Beijing
 * All Rights Reserved.
 * @version 1.0  2016年9月21日 下午6:40:23  by 杨雷（yanglei@cloud-young.com）创建
 */
@Component
public class UserCheckAndInjectIntercepter extends HandlerInterceptorAdapter{
	private static final Logger logger = LoggerFactory.getLogger(UserCheckAndInjectIntercepter.class);


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//    	String requestURI =request.getRequestURI();
//        String accessToken = request.getHeader(HeaderParams.HEADER_KEY_ACCESSTOKEN);
//		LogUtil.info(logger, "uca token={0},uri ={1}", requestURI);
//        if (StringUtils.isNotBlank(accessToken)) {
//            Long userId = TokenGenerator.analysisToken(accessToken);
//            if (userId < 0) {
//                String object2Json = JsonUtil.Object2Json(new MessageVo<>(false, AuthErrorCodeEnum.COMMON_ERROR_160019.getCode(), AuthErrorCodeEnum.COMMON_ERROR_160019.getMsg()));
//                this.write(response, requestURI, accessToken, object2Json);
//                return false;
//            }
//            UserBaseInfo userBaseInfo = llbUserApi.getLlbUserBaseInfoByUserId(userId);
//            if (Objects.isNull(userBaseInfo)) {
//                String object2Json = JsonUtil.Object2Json(new MessageVo<>(false, AuthErrorCodeEnum.COMMON_ERROR_160019.getCode(), AuthErrorCodeEnum.COMMON_ERROR_160019.getMsg()));
//                this.write(response, requestURI, accessToken, object2Json);
//                return false;
//            }
//            if (ZeroOrOneEnum.ZERO.getCode().equals(userBaseInfo.getStatus())) {
//                //被禁用
//                String object2Json = JsonUtil.Object2Json(new MessageVo<>(false, BaseUcenterErrorEnum.ERR_1000004.getCode(), BaseUcenterErrorEnum.ERR_1000004.getMsg()));
//                this.write(response, requestURI, accessToken, object2Json);
//                return false;
//            }
//            request.setAttribute("user", userBaseInfo);
//            // 记录常用地点
//            UserLoginBindingParameter bindingParameter = (UserLoginBindingParameter) request.getAttribute(ApiConstants.LOGIN_BINDING);
//            this.recordLocations(userBaseInfo, bindingParameter);
//        }
        return true;
    }

//    /**
//     * Description: 记录设备常用地点 <br>
//     * Version1.0 2019-04-22 10:22 by 宫珣（gongxun@cloud-young.com/）创建
//     * @param userBaseInfo      user
//     * @param bindingParameter  bindingParameter
//     */
//    private void recordLocations(UserBaseInfo userBaseInfo, UserLoginBindingParameter bindingParameter) {
//        String cacheKey = UcenterCacheKeys.RECORD_USER_DEVICE_AND_LOCATION + userBaseInfo.getMobile() + bindingParameter.getLastLoginIp() + userBaseInfo.getDeviceImei();
//        if (!redissonTemplete.isExists(cacheKey)) {
//            //记录常用设备/位置信息
//            UserParameter userParameter = new UserParameter();
//            userParameter.setMobile(userBaseInfo.getMobile());
//            userParameter.setUserId(userBaseInfo.getUserId());
//            userParameter.setChannelInfoId(userBaseInfo.getLoginChannelInfoId());
//            userParameter.setLastLoginIp(StringUtils.trimToEmpty(bindingParameter.getLastLoginIp()));
//            userParameter.setDeviceImei(StringUtils.trimToEmpty(bindingParameter.getDeviceImei()));
//            userParameter.setDeviceType(StringUtils.trimToEmpty(bindingParameter.getDeviceType()));
//            userParameter.setDeviceModel(StringUtils.trimToEmpty(bindingParameter.getDeviceModel()));
//            ucenterUpgradeApi.recordCommonLocationAndDevice(userParameter);
//            redissonTemplete.set(cacheKey, 1, 1, TimeUnit.HOURS);
//        }
//    }
//    /**
//     * Description: 错误返回结果 <br>
//     * Version1.0 2019-04-22 10:21 by 宫珣（gongxun@cloud-young.com/）创建
//     * @param response      response
//     * @param requestURI    requestURI
//     * @param accessToken   accessToken
//     * @param object2Json   object2Json
//     * @throws IOException  IOException
//     */
//    private void write(HttpServletResponse response, String requestURI, String accessToken, String object2Json) throws IOException {
//        LogUtil.info(logger, "uca false accessToken={0},uri={1},res ={2}", accessToken, requestURI, object2Json);
//        response.setContentType("application/json");
//        response.getWriter().write(object2Json);
//    }


}
